package com.example.project3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

    private String username; // 私有成员变量存用户名
    private Button btnLogout, btnMy;
    private TextView tvWelcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        // 接收用户名
        Intent intent = getIntent();
        if (intent != null) {
            username = intent.getStringExtra("username");
        }

        btnLogout = findViewById(R.id.btn_logout);
        btnMy = findViewById(R.id.btn_my);
        tvWelcome = findViewById(R.id.tv_welcome);

        tvWelcome.setText("欢迎 " + username + " 回来！");

        // 退出登录弹对话框
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(HomeActivity.this)
                        .setTitle("确认退出")
                        .setMessage("是否确定退出登录？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            startActivity(new Intent(HomeActivity.this, LoginActivity.class));
                            finish();
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });

        // 跳转到个人信息编辑页
        btnMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, ProfileEditActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
            }
        });
    }
}